<?php
/**
* @copyright	Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
**/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );


class TemplateAdminHTML
{

	function display_default( $params, $bannerlist = array() )
	{
		JHTML::_('behavior.tooltip');
		JHTML::_('behavior.modal');

		$no_hide = (int) JRequest::getVar('tadmin_no_hide', 0);
        ?>

        <script type="text/javascript" src="templates/<?php echo _TEMPLATE_NAME;?>/<?php echo _TMPL_ADMIN_DIRNAME;?>/template_admin.js"></script>

        <div align="center">



           <div id="templateAdmin_container" style="display:block;width:95%;height:300px;background:#FFF;padding:5px; border:5px solid #000;" align="left">

              <div style="color:#000;width:100%;height:300px;overflow:auto;">

              	<div class="inside">

                	<div id="templateAdmin_debug"></div><div class="clr"></div>

                 <div style="display:block;float:left;width:58%">

                    <fieldset>
                    <legend><?php echo JText::_('Image Overrides');?></legend>
                    <a class="modal icon-override" href="index.php?template_task=edit_images" rel="{handler: 'iframe', size: {x: 640, y: 480}}"><?php echo JText::_('Upload your custom logo, bullet or blockquote image');?></a>
                    </fieldset>
                    <fieldset>

				    <legend><?php echo JText::_('Custom Banner Images');?></legend>
				       <div style="float:right">
				       <table class="toolbar">
				          <tr>
				             <td>
				                <a class="modal" href="index.php?template_task=add_banner" rel="{handler: 'iframe', size: {x: 640, y: 480}}" style="text-align:center">
				                <span class="icon-32-new" title="<?php echo JText::_('Add');?>"></span><?php echo JText::_('Add');?>

				                </a>
				             </td>
				             <td>
				                <a href="javascript:deleteBanner();"><span class="icon-32-delete" title="<?php echo JText::_('Delete');?>"></span><?php echo JText::_('Delete');?></a>
				             </td>
				          </tr>
				       </table>
				       </div>
				       <div class="header icon-48-module"><?php echo JText::_('Banner Manager');?></div>
                       <br/>
                       <div id="banner_manager">
                       <?php TemplateAdminHTML::display_bannermanager($bannerlist); ?>
                       </div>

				    </fieldset>

                 </div>



                 <div style="display:block;float:right;width:39%;">



			        <fieldset>

				    <legend><?php echo JText::_( 'Parameters' ); ?></legend>

				    <form id="templateAdmin_Parameters" action="index.php" method="post" onsubmit="submitParamsForm();return false;">

				    <?php $templatefile = DS.'templates'.DS._TEMPLATE_NAME.DS.'params.ini';

				    if(is_writeable($templatefile)) {
				    	echo "<span class='info'>".JText::_('PARAMSWRITABLE')."</span>";
				    }
				    else {
				    	echo "<span class='alert'>".JText::_('PARAMSUNWRITABLE').": ".str_replace(JPATH_ROOT, '',$templatefile)."</span>";
				    }
				    ?>

				    <table class="admintable" width="100%">

				    <tr>

					   <td style="padding-top:3px; padding-bottom:5px;">

						 <?php

						 if (!is_null($params)) {

			                echo $params->render();

		                 } else {

			                echo '<i>' . JText :: _('No Parameters') . '</i>';

		                 }

                         ?>

					   </td>

				    </tr>

				    <tr>

				      <td><input type="submit" class="button" value="<?php echo JText::_('Apply');?>"/></td>

				    </tr>

				   </table>

				   <input type="hidden" name="template_task" value="update_params" />

				   </form>

			      </fieldset>





                 </div>

				</div>

              </div>

           </div>

           <div style="color:#fff;width:400px;font-weight:bold;"><a id="templateAdmin_toggle" style="cursor:pointer"><?php echo JText::_('Customize your template');?></a></div>

        </div>

        <script type="text/javascript">

        var slider = new Fx.Slide('templateAdmin_container');

        <?php if(!$no_hide) { ?>slider.hide();<?php } ?>

        $('templateAdmin_toggle').addEvent('click', function(e)
        {

	        e = new Event(e);

	        slider.toggle();

	        e.stop();

        });

        </script>

        <?php

	}


	function display_bannermanager($rows)
	{
		?>
		<script type="text/javascript">
		window.addEvent('domready', function() {

			SqueezeBox.initialize({});

			$$('a.modal').each(function(el) {
				el.addEvent('click', function(e) {
					new Event(e).stop();
					SqueezeBox.fromElement(el);
				});
			});
		});
		</script>
        <form id="form_bannermanager" name="form_bannermanager" method="post" action="index.php">
		<table class="adminlist" cellspacing="1">
		   <thead>
		   <tr>
		      <th class="title" width="5">#</th>
			  <th class="title" width="5">&nbsp;</th>
			  <th class="title" width="50%" align="left"><?php echo JText::_('Banner Name');?></th>
			  <th class="title" width="50%" align="left"><?php echo JText::_('Menu Item');?></th>
		   </tr>
		   </thead>
		   <tbody>
		   <?php
		   $k = 0;
		   foreach ($rows AS $i => $row)
		   {
				?>
				<tr class='row<?php echo $k;?>'>
				   <td width="5"><?php echo $i+1;?></td>
				   <td align="center" width="5"><input type="checkbox" name="cid[]" id="cb<?php echo $i;?>" value="<?php echo $row->id;?>" onclick="isChecked2(this.checked,'form_bannermanager');"/></td>
				   <td width="50%"><a class="modal" href="index.php?template_task=edit_banner&id=<?php echo $row->id;?>" style="color:#000" rel="{handler: 'iframe', size: {x: 640, y: 480}}"><?php echo htmlspecialchars($row->title);?></a></td>
				   <td width="50%"><a href="<?php echo JRoute::_($row->link."&tadmin_no_hide=1&Itemid=".$row->mid);?>" style="color:#000"><?php echo htmlspecialchars($row->name.' ('.$row->menutype.')');?></a></td>
				</tr>
				<?php
				$k = 1 - $k;
		   }
		   if(!count($rows)) {
		   	  ?>
		   	  <tr>
		   	     <td colspan="4"><span class="info"><?php echo JText::_('No custom banners uploaded yet');?></span></td>
		   	  </tr>
		   	  <?php
		   }
		   ?>
		   </tbody>
		</table>
		<input name="boxchecked" type="hidden" value=""/>
		</form>
		<?php
	}


	function display_addBanner($rows)
	{
		?>
		<script type="text/javascript" src="templates/<?php echo _TEMPLATE_NAME;?>/<?php echo _TMPL_ADMIN_DIRNAME;?>/template_admin.js"></script>
		<form id="templateAdmin_addBanner" name="templateAdmin_addBanner" method="post" enctype="multipart/form-data" action="index.php" onsubmit="addBanner(this); return false;">
		<div class="header icon-48-module"><?php echo JText::_('Add Banner');?></div><br/>
		<table class="adminlist" cellspacing="1">
		<thead>
		   <tr>
		      <th class="title"><input type="file" name="banner" id="banner_img"/></th>
		      <th class="title" style="font-size:11px"><?php echo JText::_('Select your Banner and assign it to a menu item below');?></th>
		      <th class="title"><input type="submit" value="<?php echo JText::_('Submit');?>" /></th>
		   </tr>
		</thead>
		</table>
		<?php
        if(is_writeable(JPATH_TMPL_BANNER)) {
           echo "<span class='info'>".JTEXT::_('Banner directory is writeable').": ".str_replace(JPATH_ROOT, '',JPATH_TMPL_BANNER)."</span>";
        }
        else {
           echo "<span class='alert'>".JTEXT::_('Banner directory is unwriteable').": ".str_replace(JPATH_ROOT, '',JPATH_TMPL_BANNER)."</span>";
        }
        ?>
		<table class="adminlist" cellspacing="1">
		   <thead>
		      <tr>
		         <th class="title" width="5">#</th>
		         <th class="title" width="5">&nbsp;</th>
		         <th class="title" width="5"><?php echo JText::_('Id');?></th>
		         <th class="title" width="50%"><?php echo JText::_('Menu Item');?></th>
		         <th class="title" width="50%"><?php echo JText::_('Menu Name');?></th>
		      </tr>
		   </thead>
		   <tbody>
		      <?php
		      $k = 0;
		      foreach ($rows AS $i => $row)
		      {
		      	 $block  = '';

		      	 if($row->occupied) { $block  = "disabled='disabled'"; }
		      	 ?>
		      	 <tr class="row<?php echo $k;?>">
		      	    <td><?php echo $i + 1;?></td>
		      	    <td><input type="checkbox" value="<?php echo $row->id;?>" id="cb<?php echo $i;?>" name="cid[]" onclick="isChecked2(this.checked, 'templateAdmin_addBanner');" <?php echo $block;?>/></td>
		      	    <td><?php echo $row->id;?></td>
		      	    <td><?php echo htmlspecialchars($row->name);?></td>
		      	    <td><?php echo htmlspecialchars($row->menutype);?></td>
		      	 </tr>
		      	 <?php
		      	 $k = 1 - $k;
		      }
		      ?>
		   </tbody>
		</table>
		<input type="hidden" name="boxchecked" id="bx"/>
		<input type="hidden" name="template_task" value="save_banner" />
		</form>
		<?php
	}


	function display_editBanner(&$rows, &$row)
	{
		$banner = $row;
		?>
		<script type="text/javascript" src="templates/<?php echo _TEMPLATE_NAME;?>/<?php echo _TMPL_ADMIN_DIRNAME;?>/template_admin.js"></script>
		<form id="templateAdmin_addBanner" name="templateAdmin_addBanner" method="post" enctype="multipart/form-data" action="index.php" onsubmit="updateBanner(this); return false;">
		<div class="header icon-48-module"><?php echo JText::_('Edit Banner')." :: ".htmlspecialchars($row->title);?></div><br/>
		<table class="adminlist" cellspacing="1">
		<thead>
		   <tr>
		      <th class="title"><input type="file" name="banner" id="banner_img"/></th>
		      <th class="title" style="font-size:11px"><?php echo JText::_('Select your Banner and assign it to a menu item below');?></th>
		      <th class="title"><input type="submit" value="<?php echo JText::_('Submit');?>" /></th>
		   </tr>
		</thead>
		</table>
		<?php
        if(is_writeable(JPATH_TMPL_BANNER)) {
           echo "<span class='info'>".JTEXT::_('Banner directory is writeable').": ".str_replace(JPATH_ROOT, '',JPATH_TMPL_BANNER)."</span>";
        }
        else {
           echo "<span class='alert'>".JTEXT::_('Banner directory is unwriteable').": ".str_replace(JPATH_ROOT, '',JPATH_TMPL_BANNER)."</span>";
        }
        ?>
		<table class="adminlist" cellspacing="1">
		   <thead>
		      <tr>
		         <th class="title" width="5">#</th>
		         <th class="title" width="5">&nbsp;</th>
		         <th class="title" width="5"><?php echo JText::_('Id');?></th>
		         <th class="title" width="50%"><?php echo JText::_('Menu Item');?></th>
		         <th class="title" width="50%"><?php echo JText::_('Menu Name');?></th>
		      </tr>
		   </thead>
		   <tbody>
		      <?php
		      $k = 0;
		      foreach ($rows AS $i => $row)
		      {
		      	 $block  = '';
                 $checked = '';

                 if($row->id == $banner->menu_item) { $checked = 'checked="checked"'; }

		      	 if($row->occupied && !$checked) {
		      	 	$block  = "disabled='disabled'";
		      	 }

		      	 ?>
		      	 <tr class='row<?php echo $k;?>'>
		      	    <td><?php echo $i + 1;?></td>
		      	    <td><input type="checkbox" value="<?php echo $row->id;?>" id="cb<?php echo $i;?>" name="cid[]" onclick="isChecked2(this.checked, 'templateAdmin_addBanner');" <?php echo $block;?> <?php echo $checked;?>/></td>
		      	    <td><?php echo $row->id;?></td>
		      	    <td><?php echo htmlspecialchars($row->name);?></td>
		      	    <td><?php echo htmlspecialchars($row->menutype);?></td>
		      	 </tr>
		      	 <?php
		      	 $k = 1 - $k;
		      }
		      ?>
		   </tbody>
		</table>
		<input type="hidden" name="boxchecked" id="bx" value="1"/>
		<input type="hidden" name="template_task" value="update_banner" />
		<input type="hidden" name="id" value="<?php echo $banner->id;?>" />
		</form>
		<?php
	}


	function display_images($images)
	{
		require_once(JPATH_ROOT.DS.'components'.DS.'com_media'.DS.'helpers'.DS.'media.php');
		?>
		<form name="templateAdmin_images" method="post" enctype="multipart/form-data" action="index.php">
		<div class="header icon-48-media"><?php echo JText::_('Image Overrides');?></div><br/>
        <table class="adminlist" cellspacing="1">
           <thead>
              <th class="title" width="5">#</th>
              <th class="title" width="40%"><?php echo JText::_('Template Image');?></th>
              <th class="title" width="60%"><?php echo JText::_('Replace with');?></th>
           </thead>
           <?php
           $k = 0;
           foreach ($images AS $i => $image)
           {
           	   $name = htmlspecialchars($image);
           	   $size = getimagesize(JPATH_TMPL_IMAGES.DS.$image);
           	   $height = $size[0];
           	   $width  = $size[1];
           	   ?>
           	   <tr class="row<?php echo $k;?>">
           	      <td><?php echo $i + 1;?></td>
           	      <td>
           	         <div align="center">
           	            <?php echo "<img src='templates/"._TEMPLATE_NAME."/images/$name' border='0' alt='$name' ".MediaHelper::imageResize($width, $height, 56)."/>";?>
           	            <br/>
           	            <?php echo $name;?>
           	         </div>
           	      </td>
           	      <td><input type="file" name="image_<?php echo $i;?>" /></td>
           	   </tr>
           	   <?php
           	   $k = 1-$k;
           }
           ?>
           <tr>
              <td colspan="3" align="center"><input type="submit" value="<?php echo JText::_('Submit');?>" class="button"/></td>
           </tr>
        </table>
        <input type="hidden" name="template_task" value="update_images" />
        </form>
		<?php
	}


	function display_jslanguage()
	{
		?>
		<script type="text/javascript">
		var lang_tmpl_alert_select_banner = "<?php echo JText::_('Please select a banner');?>";
		var lang_tmpl_alert_select_item   = "<?php echo JText::_('Please select a menu item');?>";
		</script>
		<?php
	}
}

?>